/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ui;

import control.MStockMidlet;
import font.MyFont;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;
import listener.Command;
import listener.ICommandListener;
import util.SoftKey;

/**
 *
 * @author kien.tran
 */
public class CustomGauge extends Canvas {

    private int width, height, advAreaHeight;
    private int x, y;
    private int progressBarHeight, progressBarWidth;
    private int item;
    private int w_painted;
    private Image background, advertise;
    private MyFont font;
    private Style style, footerStyle;
    private ICommandListener listener;
    private Command back;
    private String[] data;
    private boolean isLoad = false;

    public CustomGauge(Image image, String[] array, MStockMidlet control) {
        setFullScreenMode(true);

        this.background = image;
        this.data = array;
        this.font = control.getFont();
        this.style = control.getStyle().getGaugeStyle();
        this.style.setHeight(font.getFontBold().getHeight() + style.getPadding().getTopAndBottom());
        this.footerStyle = control.getStyle().getFooterStyle();
        this.footerStyle.setHeight(font.getFontBold().getHeight() + footerStyle.getPadding().getTopAndBottom());
        this.width = getWidth();
        this.height = getHeight();
        this.progressBarHeight = font.getFontNormal().getHeight() + 4;
        this.progressBarWidth = this.width - style.getPadding().getLeftAndRight();
        this.item = (int) (progressBarWidth / 100);
        this.advAreaHeight = height - (style.getHeight() + footerStyle.getHeight() + progressBarHeight);
        this.advertise = control.getAdvertise();
    }

    public void setProgress(int percent) {
        this.w_painted = this.item * percent;

        if ((!isLoad) && (percent > 1)) {
            isLoad = true;
        }
        repaint();
    }

    public void paint(Graphics g) {
        setFullScreenMode(true);

        if (this.w_painted >= this.progressBarWidth) {
            return;
        }

        g.setColor(style.getBgColor());
        g.fillRect(0, 0, width, height);

        // paint background
        if (background != null) {
            this.x = (width - background.getWidth()) / 2;
            this.y = (height - background.getHeight()) / 2;
            g.drawRegion(background, 0, 0, background.getWidth(), background.getHeight(), Sprite.TRANS_NONE, x, y, Graphics.TOP | Graphics.LEFT);
        }

        this.x = style.getPadding().getLeft();
        this.y = style.getPadding().getTop();

        font.getFontBold().drawString(g, data[0], x, y, Graphics.TOP | Graphics.LEFT);

        this.y += font.getFontBold().getHeight() + style.getPadding().getBottom();

        g.setColor(style.getBorderColor());
        g.drawRect(x - 1, y, progressBarWidth + 1, progressBarHeight + 1);
        g.setColor(style.getSelectedBgColor());
        g.fillRect(x, y + 1, w_painted, progressBarHeight);
        g.setColor(style.getBgColor());
        if (progressBarWidth > w_painted) {
            g.fillRect(w_painted + x, y + 1, progressBarWidth - w_painted, progressBarHeight);
        }

        this.y += 3;
        if (isLoad) {
            font.getFontNormal().drawString(g, data[2], this.width / 2, this.y, Graphics.TOP | Graphics.HCENTER);

        } else {
            font.getFontNormal().drawString(g, data[1], this.width / 2, this.y, Graphics.TOP | Graphics.HCENTER);
        }

        g.setColor(footerStyle.getBgColor());
        g.fillRect(0, this.height - footerStyle.getHeight(), this.width, footerStyle.getHeight());
//        boldFont.drawString(back.getText(), this.width - footerStyle.getPadding().getRight(), this.height - footerStyle.getHeight() + footerStyle.getPadding().getTop(), Font2.RIGHT, g);
        font.getFontBold().drawString(g, back.getText(), this.width - footerStyle.getPadding().getRight(), this.height - footerStyle.getHeight() + footerStyle.getPadding().getTop(), Graphics.TOP | Graphics.RIGHT);

        if (advertise != null) {
            this.x = (width - advertise.getWidth()) / 2;
            this.y = (advAreaHeight - advertise.getHeight()) / 2 + style.getHeight() + progressBarHeight;
            g.drawRegion(advertise, 0, 0, advertise.getWidth(), advertise.getHeight(), Sprite.TRANS_NONE, x, y, Graphics.TOP | Graphics.LEFT);
        }
    }

    public void setBackCommand(Command cmd) {
        this.back = cmd;
    }

    public void setListener(ICommandListener listener) {
        this.listener = listener;
    }

    public void keyPressed(int key) {
        if (key == SoftKey.getRightSoftkeyCode(this)) {
            listener.commandAction(back);
        }
    }

    public void show() {
        MStockMidlet.display.setCurrent(this);
    }
}
